package com.gid.rcflightlogger;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import android.content.Context;
import android.widget.Toast;

public class myUtils {
	
	//expected input format: year = xxxx, month in 0-11, day in 1,31
	public static String Date2Str(int year, int month, int day)
	{
		StringBuilder str = new StringBuilder();
		str.append(year).append("-");
		month = month + 1;
		if (month < 10)
			str.append(0);
		str.append(month).append("-");
		if (day < 10)
			str.append(0);
		str.append(day);
		return str.toString();
	}
	
	public static String Date2Str(Date d) 
	{
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
		return dateFormat.format(d);
	}
	
	public static String DateTime2Str(Date d) 
	{
		SimpleDateFormat dateFormat = new SimpleDateFormat("[E] yyyy-MM-dd HH:mm");
		return dateFormat.format(d);
	}
	
	//expected format yyyy-mm-dd
	public static Date Str2Date(String str)
	{
		Date d;
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
		try
	    {
	    	d = dateFormat.parse(str);
	    }
		catch (ParseException e)
		{
			//do nothing, return today's date LOL
			d = new Date();
		}
		return d;
	}
	
	public static Date makeDate(int year, int month, int day)
	{
		return new Date(year - 1900, month, day);
	}
	
	public static void makeToast(Context context, String str)
	{
        Toast toast = Toast.makeText(context, str, Toast.LENGTH_LONG);
        toast.show();
	}
	
	public static void makeToastShort(Context context, String str)
	{
        Toast toast = Toast.makeText(context, str, Toast.LENGTH_SHORT);
        toast.show();
	}
	
	public static String makeAgeStr(Date d)
	{
		long today = (new Date()).getTime();
		long then = d.getTime();
		long days = ((today - then)/1000L)/(24*60*60);
		if (days <= 0)
			return "0 days";
		else if (days < 100)
			return Long.toString(days) + " days";
		else
			return Long.toString(days/30) + " months";
	}
	
	public static String Duration2Str(int secs, boolean always_include_hours)
	{
		StringBuilder str = new StringBuilder();
		int hours = secs/(60*60);
		if (hours <= 0)
		{
			if (always_include_hours)
				str.append("00:");
		}
		else
		{
			if (hours < 10)
				str.append("0");
			str.append(Integer.toString(hours)).append(":");
		}
		
		int minutes = (secs%(60*60))/60;
		if (minutes <= 0)
		{
			str.append("00:");
		}
		else
		{
			if (minutes < 10)
				str.append("0");
			str.append(Integer.toString(minutes)).append(":");
		}
		
		int seconds = secs%60;
		if (seconds <= 0)
		{
			str.append("00");
		}
		else
		{
			if (seconds < 10)
				str.append("0");
			str.append(Integer.toString(seconds));
		}
		
		return str.toString();
	}
}
